﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;


public class AvatarHandle : IAvatarHandle
{
    public AvatarModule Module;   
    public AvatarMovement Movement;
    public AvatarAction Action;
    public AvatarAnimation Animation;
    public AvatarClothes Clothes;
    public InputInstance InputHandle;
    public GameObject WeaponGameObject;
    public GameObject Root;
    public bool IsOwner = false;
    public TaskHandle LoadComplete;
    public bool RemoteInit = false;
    public JobType Job = JobType.Soldier;

    private int id = 0;
    private string name = string.Empty;
    private Transform nameTagTransform;
    private Transform headTransform;
    private int hp;
    private int allHp;
    private int mp;
    private int allMp;
    public AvatarHandle(AvatarModule _module, bool _isOwner, int _id, string _name, string _para)
    {
        Module = _module;
        Root = GameObject.Instantiate(Resources.Load("Avatar/robot_pre"), GameObject.Find("scene001/collider/zero").transform.position, Quaternion.identity) as GameObject;
        nameTagTransform = AssetHelp.FindChildObject("NameTag", Root.transform, "");
        headTransform = AssetHelp.FindChildObject("Bip01 HeadNub", Root.transform, "");
        Root.name = Define.PlayerRootFirstName + _id;
        Movement = new AvatarMovement(this);
        Action = new AvatarAction(this);
        Animation = new AvatarAnimation(this);
        Clothes = new AvatarClothes(this);
        allHp = hp = 200;
        IsOwner = _isOwner;
        if (IsOwner)
        {
            InputHandle = InputManager.OwnerInput;
            WeaponGameObject = AssetHelp.FindChildObject("Dao", Root.transform, "").gameObject;
        }
        else InputHandle = new InputInstance();
        id = _id;
        name = _name;
        Clothes.Para = _para;

        WaitHandle.Start(0.5f, delegate()
        {
            if (LoadComplete != null) LoadComplete();
        });
    }
    public void Update() 
    {
        Movement.Update();
        Action.Update();
        Animation.Update();
    }
    public void FixedUpdate()
    {
        Movement.FixedUpdate();
        Clothes.FixedUpdate();
    }
    #region IAvatarHandle Members
    public Vector3 Position
    {
        get { return Root.transform.position; }
    }

    public int Id
    {
        get { return id; }
    }
    public event PositionChangeHandler OnPositionChange;
    public void PositionChange(IUnit unit, Vector3 pos)
    {
        if(OnPositionChange != null) OnPositionChange(unit, pos);
    }
    public string Name
    {
        get { return name; }
    }

    public Transform NameTagTransform
    {
        get { return nameTagTransform; }
    }

    public Transform HeadTransform
    {
        get { return headTransform; }
    }
    public Collider Collider
    {
        get { return Root.collider; }
    }
    public Collider Weapon
    {
        get { return WeaponGameObject.collider; }
    }

    public int Mp
    {
        get { return mp; }
    }

    public int AllMp
    {
        get { return allMp; }
    }

    public int Hp
    {
        get { return hp; }
    }

    public int AllHp
    {
        get { return allHp; }
    }

    public void DoAction(UInt16 actionId, bool net)
    {
        Action.DoAction(actionId, net);
    }

    public void MpChange(IAvatarHandle handle, int tMp)
    {
        if (OnMpChange != null) OnMpChange(handle, tMp);
    }
    public event Action<IAvatarHandle, int> OnMpChange;

    public void HpChange(IUnit handle, int tHp)
    {
        if (OnHpChange != null) OnHpChange(handle, tHp);
    }
    public event Action<IUnit, int> OnHpChange;

    public void DoDead(IUnit deader, IUnit killer)
    {
        if (OnDead != null) OnDead(deader, killer);
    }
    public event Action<IUnit, IUnit> OnDead;

    public void OnBeat(ActionBase _ation, int _hp, IUnit _from, bool _net)
    {
        Action.OnBeat(_ation, _hp, _net);
        hp += _hp;
        if (hp < 0) hp = 0;
        HpChange(this as IUnit, _hp);
        if (hp <= 0)
        {
            OnDead(this as IUnit, _from);
        }
    }

    private bool deadReady = false;
    private bool dead = false;
    public bool Dead
    {
        get { return dead; }
    }
    private Vector2 excursion = Vector2.zero;
    public Vector2 Excursion
    {
        get { return excursion; }
        set { excursion = value; }
    }

    public Vector2 ReduceExcursion
    {
        get { return Vector2.right * .3f; }
    }
    public float Friction
    {
        get { return .3f; }
    }
    public MovementType Direction
    {
        get { return Movement.Direction; }
        set { Movement.Direction = value; }
    }

    public void CrossAnimation(string animationName, float time)
    {
        Animation.PlayAnimation(true, animationName);
    }
    public bool Gravity
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public CharacterController Controller
    {
        get { return Movement.Controller.Control; }
    }

    public bool DoExcursion
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    #endregion
}

